local bushi = fk.CreateSkill{
    name = "ld__bushi",
}

local H = require "packages.ol_hegemony.util"

bushi:addEffect(fk.Damaged,{
    mute = true,
    trigger_times = function(self, event, target, player, data)
        return data.damage
    end,
    can_trigger = function(self,event,target,player,data)
       return player:hasSkill(bushi.name) and not player.dead and data.to == player
    end,
    on_cost = function(self,event,target,player,data)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, player) end)
        local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#ld__bushi-choose",
            skill_name = bushi.name,
            cancelable = true,
        })
        if #to > 0 then
          event:setCostData(self, {to = to})
          return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room =player.room
        player:broadcastSkillInvoke(bushi.name)
        room:notifySkillInvoked(player, bushi.name, "drawcard")
        event:getCostData(self).to[1]:drawCards(1, bushi.name)
    end,
})

bushi:addEffect(fk.Damage,{
    mute = true,
    can_trigger = function(self,event,target,player,data)
          return player:hasSkill(bushi.name) and not data.to.dead and player.kingdom ~= "unknown" and data.from == player
          and data.to.kingdom ~= "unknown" and data.to ~= player
    end,
    on_cost = function(self,event,target,player,data)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, data.to) end)
        if player:hasShownSkill(bushi.name) then
          local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#ld__bushi-choose",
            skill_name = bushi.name,
            cancelable = false,
          })
          if #to > 0 then
            event:setCostData(self, {to = to})
            return true
          end
          else
          local to = room:askToChoosePlayers(player,{
                targets = targets,
                min_num = 1,
                max_num = 1,
                prompt = "#ld__bushi-choose",
                skill_name = bushi.name,
                cancelable = true,
          })
          if #to > 0 then
            event:setCostData(self, {to = to})
            return true
          end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room =player.room
        player:broadcastSkillInvoke(bushi.name)
        room:notifySkillInvoked(player, bushi.name, "drawcard")
        event:getCostData(self).to[1]:drawCards(1, bushi.name)
    end,

})

Fk:loadTranslationTable{
    ["ld__bushi"] = "布施",
    [":ld__bushi"] = "当你受到1点伤害后，你可以令一名与你势力相同的角色摸一张牌；当你对有势力的其他角色造成伤害后，你令一名与其势力相同的角色摸一张牌。",
    ["#ld__bushi-choose"]="布施：你可令一名角色摸一张牌",

    ["$ld__bushi1"] = "争斗，永远没有赢家。",
    ["$ld__bushi2"] = "和平，永远没有输家。",
}

return bushi